package com.library.management.controller.notice;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.management.common.Result;
import com.library.management.domain.notice.Notice;
import com.library.management.service.notice.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    
    @Autowired
    private NoticeService noticeService;
    
    /**
     * 获取启用的公告列表（
     */
    @GetMapping("/active")
    public Result<List<Notice>> getActiveNotices(
            @RequestParam(defaultValue = "5") Integer limit) {
        try {
            List<Notice> result = noticeService.getActiveNotices(limit);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取公告列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 发布公告
     */
    @PostMapping("/publish")
    public Result<String> publishNotice(@RequestBody Notice notice) {
        try {
            boolean success = noticeService.publishNotice(notice);
            if (success) {
                return Result.success("发布公告成功");
            } else {
                return Result.error("发布公告失败");
            }
        } catch (Exception e) {
            return Result.error("发布公告失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除公告
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteNotice(@PathVariable Long id) {
        try {
            boolean success = noticeService.deleteNotice(id);
            if (success) {
                return Result.success("删除公告成功");
            } else {
                return Result.error("删除公告失败");
            }
        } catch (Exception e) {
            return Result.error("删除公告失败: " + e.getMessage());
        }
    }
} 